package com.indexingsystem.system.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.AuthRoleVo;
import com.indexingsystem.boss.Vo.RolesDropVo;
import com.indexingsystem.boss.Vo.RolesListVo;
import com.indexingsystem.boss.Vo.RolesVo;
import com.indexingsystem.boss.admin.mapper.RoleOperationsMapper;
import com.indexingsystem.boss.admin.mapper.RolesMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.Roles;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IRolesService;
import com.indexingsystem.system.web.model.Role;

@Service
public class RolesServiceImpl implements IRolesService {

	@Resource
	private RolesMapper rolesMapper;

	@Resource
	private RoleOperationsMapper roleOperationsMapper;

	/**
	 * 通过角色id删除角色信息.
	 * 
	 * @param roleId
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(String roleId) {
		return rolesMapper.deleteByPrimaryKey(roleId);
	}

	/**
	 * 批量删除角色操作.
	 * 
	 * @param roleIdList
	 * @return
	 */
	@Override
	public int deleteRolesByIdBatch(List<String> roleIdList)  throws Exception{
		// 1、删除角色，需要清理角色操作关系表
		roleOperationsMapper.deleteBatchRoleOperationsByRoleIds(roleIdList);
		return rolesMapper.deleteRolesByIdBatch(roleIdList);
	}

	/**
	 * 新增角色信息.
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int insertSelective(Roles record) {
		return rolesMapper.insertSelective(record);
	}

	/**
	 * 通过角色id查询角色信息.
	 * 
	 * @param roleId
	 * @return
	 */
	@Override
	public Roles selectByPrimaryKey(String roleId) {
		return rolesMapper.selectByPrimaryKey(roleId);
	}

	/**
	 * 修改角色信息.
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(Roles record) {
		return rolesMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 获取角色列表.
	 * 
	 * @return
	 */
	@Override
	public List<Roles> getRolesList() {
		return rolesMapper.getRolesList();
	}

	/**
	 * 分页查询角色列表.
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public PagedResult<RolesListVo> selectRolesList(String queryCriteria,Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(rolesMapper.selectRolesList(queryCriteria));
	}

	/**
	 * 通过userId获取角色列表信息.
	 */
	@Override
	public List<Roles> getRoleInfoByUserId(String userId) {
		return rolesMapper.getRoleInfoByUserId(userId);
	}

	/**
	 * 新增用户管理初始化角色下来控件列表.
	 * 
	 * @return
	 */
	@Override
	public List<RolesDropVo> getRolesDropVoList() {
		return rolesMapper.getRolesDropVoList();
	}

	/**
	 * 查询角色列表，条件为不包含userId的其它可以选择绑定的角色列表.
	 */
	@Override
	public List<Roles> getCheckRoles(String userId) {
		return rolesMapper.getCheckRoles(userId);
	}

	/**
	 * 根据用户ID查询角色（role），放入到Authorization里.
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public List<AuthRoleVo> findRoleByUserId(String userId) {
		return rolesMapper.findRoleByUserId(userId);
	}

	/**
	 * 获取角色总数量.
	 * 
	 * @return
	 */
	@Override
	public Integer getRoleCount(String queryCriteria) {
		return rolesMapper.getRoleCount(queryCriteria);
	}

	
	
	   /**
     * 通过角色名称查询角色信息.
     * 
     * @param roleName
     * @return
     */
    @Override
    public Roles getRolesInfoByRoleName(String roleName){
        return rolesMapper.getRolesInfoByRoleName(roleName);
    }
    
    
    /**
     * 通过角色code查询角色信息.
     * @param roleCode
     * @return
     */
    @Override
    public Roles getRolesInfoByRoleCode(String roleCode){
        return rolesMapper.getRolesInfoByRoleCode(roleCode);
    }

	/**
	 * 通过角色id查询角色授权url及角色code.
	 * 
	 * @param roleId
	 * @return
	 */
	@Override
	public List<AuthRoleVo> getRolesCodeOrPageUrl(String roleId) {
		return rolesMapper.getRolesCodeOrPageUrl(roleId);
	}

	/**
	 * 通过用户名查询角色id集合.
	 * 
	 * @param userName
	 * @return
	 */
	@Override
	public Set<String> getRoleIdByUserName(String userName) {
		return rolesMapper.getRoleIdByUserName(userName);
	}
	
	/**
	 * 通过角色组id获取角色组角色列表.
	 * @param roleGroupId
	 * @return
	 */
	@Override
    public	List<Roles> getRolesListByRoleGroupId(String roleGroupId){
		return rolesMapper.getRolesListByRoleGroupId(roleGroupId);
	}
	
    /**
     * 查询是否已设置自动分配的角色.
     * @param autoAssignment
     * @return
     */
	@Override
    public String getRolesInfoByAutoAssignment(Integer autoAssignment){
         return rolesMapper.getRolesInfoByAutoAssignment(autoAssignment);
    }

	@Override
	public List<Roles> getdhRolesList() {
		// TODO Auto-generated method stub
		return rolesMapper.getdhRolesList();
	}
}